package com.ruoyi.event.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.event.domain.bo.EventBfResultBo;
import com.ruoyi.event.domain.vo.EventBfResultVo;
import com.ruoyi.event.service.IEventBfResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 帮扶成效
 *
 * @author strongme
 * @date 2023-02-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/bfResult")
public class EventBfResultController extends BaseController {

    private final IEventBfResultService iEventBfResultService;

    /**
     * 查询帮扶成效列表
     */
    @GetMapping("/list")
    public TableDataInfo<EventBfResultVo> list(EventBfResultBo bo, PageQuery pageQuery) {
        return iEventBfResultService.queryPageList(bo, pageQuery);
    }

    @PostMapping("query")
    public R<List<EventBfResultVo>> query(@RequestBody EventBfResultBo bo) {
        return R.ok(iEventBfResultService.queryList(bo));
    }

    /**
     * 导出帮扶成效列表
     */
    @Log(title = "帮扶成效", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventBfResultBo bo, HttpServletResponse response) {
        List<EventBfResultVo> list = iEventBfResultService.queryList(bo);
        ExcelUtil.exportExcel(list, "帮扶成效", EventBfResultVo.class, response);
    }

    /**
     * 获取帮扶成效详细信息
     *
     * @param bfResultId 主键
     */
    @GetMapping("/{bfResultId}")
    public R<EventBfResultVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long bfResultId) {
        return R.ok(iEventBfResultService.queryById(bfResultId));
    }

    /**
     * 新增帮扶成效
     */
    @Log(title = "帮扶成效", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventBfResultBo bo) {
        return toAjax(iEventBfResultService.insertByBo(bo));
    }

    /**
     * 修改帮扶成效
     */
    @Log(title = "帮扶成效", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventBfResultBo bo) {
        return toAjax(iEventBfResultService.updateByBo(bo));
    }

    /**
     * 删除帮扶成效
     *
     * @param bfResultIds 主键串
     */
    @Log(title = "帮扶成效", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bfResultIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] bfResultIds) {
        return toAjax(iEventBfResultService.deleteWithValidByIds(Arrays.asList(bfResultIds), true));
    }
}
